<template>
  <div class="login-container">
    <!-- 视频 -->
    <video
      class="login-video"
      type="video/mp4"
      autoplay
      muted
      loop
      src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4"
    />

    <!-- 登录表单 -->
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="logo-container">
        <img class="logo" src="../../assets/login-logo.png">
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>

        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="账号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-form-item prop="code" class="form-item-code">
        <span class="svg-container">
          <svg-icon icon-class="auth-code" />
        </span>

        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="验证码"
          name="code"
          type="text"
          tabindex="1"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />

        <img v-if="codeImageUrl" class="code-image" :src="codeImageUrl" @click="getCodeImage">
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="height: 54px; width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <div class="tips">
        <span>仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！</span>
      </div>
    </el-form>

    <span
      class="bottom-info"
      v-html="'江苏传智播客教育科技股份有限公司&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;版权所有Copyright2006-2022 All RightsReserved&nbsp;&nbsp;&nbsp;&nbsp;苏ICP备16007882号-11'"
    />
  </div>
</template>

<script>
import { getUUID } from '@/utils/unique'
import { getCaptcha } from '@/api/user'
import { getLoginKey } from '@/utils/login-key'

export default {
  name: 'Login',
  data() {
    const validateAccount = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    return {
      // 验证码图片Url
      codeImageUrl: '',
      loginForm: {
        account: 'demo',
        password: '888itcast.CN764%...',
        code: '',
        // 客户端唯一Key
        key: getLoginKey(getUUID()),
        tenant: '0000'
      },
      loginRules: {
        account: [
          { required: true, trigger: 'blur', validator: validateAccount }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        code: [
          { required: true, trigger: 'blur', validator: validateCode }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 获取验证码
    this.getCodeImage()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 获取验证码
    async getCodeImage() {
      const result = await getCaptcha({
        key: this.loginForm.key
      })
      this.codeImageUrl = URL.createObjectURL(result)
    },
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push('/')
              this.loading = false
            })
            .catch(() => {
              this.loading = false
              // 登录失败，重新获取验证码
              this.getCodeImage()
            })
        } else {
          console.log('表单验证 => 失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
// 光标颜色
$cursor: #889aa4;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    border-color: rgba(0, 0, 0, 0.2);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<!-- ================================================================================================================================= -->

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.tips {
    font-size: 14px;
    color: red;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

::v-deep .el-input {
  input {
    color: #000;
  }
}

.login-container {
  position: relative;
  width: 100%;
  min-height: 100%;
  overflow: hidden;

  // 视频
  .login-video {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    // 让视频铺满全屏
    object-fit: fill;
  }

  // 表单
  .login-form {
    position: absolute;
    left: 50%;
    top: 50%;
    background-color: #fff;
    border-radius: 10px;
    width: 520px;
    padding: 36px;
    max-width: 100%;
    overflow: hidden;
    transform: translate(-50%, -50%);

    .logo-container {
      text-align: center;
      padding-bottom: 28px;

      .logo {
        height: 54px;
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    ::v-deep .form-item-code {
      position: relative;

      .code-image {
        position: absolute;
        right: 0;
        top: 50%;
        width: 80px;
        height: 32px;
        transform: translateY(-50%);
        margin-left: 10px;
        margin-right: 10px;
      }
    }
  }

  // 底部信息
  .bottom-info {
    position: absolute;
    width: 100%;
    bottom: 0;
    left: 0%;
    color: #fff;
    font-size: 14px;
    line-height: 14px;
    opacity: 0.9;
    text-align: center;
    margin-bottom: 17px;
  }
}
</style>
